// pages/enterprise/index/home/index.js
import Config from "../../../../main.config";
import {getStorageSync} from "../../../../utils/Storage";
import {getPublisherList, getRecruitmentList, getPublishPositionList} from "../../../../service/Position";
import Notification from "../../../../utils/Notification"
Component({

    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true,
        styleIsolation: "shared",
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    lifetimes: {
        attached: function () {
            let companyId = getStorageSync(Config.storage.registerInfo + ".id");
            this.getPublishers();
            this.data._search = {companyId: companyId};
            this.data._companyId = companyId;
            this.getPositionTypes();
            this.setData({"_recruitment.loading": true})
        }
    },

    pageLifetimes: {
        show: function () {
            this.setData({"_recruitment.reset": true})
        }
    },


    /**
     * 组件的初始数据
     */
    data: {
        //职位
        _position: {
            list: [
                { text: "职位类型", value: 0 }
            ],
            current: 0
        },
        //发布人
        _publisher: {
            list: [
                { text: "发布人", value: 0 },
                {
                    text: "本人发布",
                    value: 1,
                    search: getStorageSync(Config.storage.userInfo + ".userId")
                }
            ],
            current: 0
        },
        //招聘信息
        _recruitment: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },



    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 获取职位类型
        * */
        getPositionTypes: async function () {
            try {
                let companyId = this.data._companyId;
                let res = await getPublishPositionList({companyId});
                let list = this.data._position.list;
                if (res.length === 0) return false;
                let newList = res.map(function (item, index) {
                    return {
                        text: item.name,
                        value: index + 1,
                        search: item.name
                    }
                })
                this.setData({
                    "_position.list": list.concat(newList)
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取发布人列表
        * */
        getPublishers: async function () {
            try {
                let res = await getPublisherList();
                let list = this.data._publisher.list;
                if (res.length === 0) return false;
                let newList = res.map(function (item, index) {
                    return {
                        text: item.positionName,
                        value: index + 2,
                        search: item.id
                    }
                })
                this.setData({
                    "_publisher.list": list.concat(newList)
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 列表加载
        * */
        onLoadList: async function (e) {
            try {
                let search = this.data._search;
                let paging = e.detail.paging;
                let list = this.data._recruitment.list;
                let isReset = this.data._recruitment.reset;
                let postData = Object.assign(search, {
                    page: paging.page,
                    limit: paging.pageSize,
                })
                let res = await getRecruitmentList(postData);
                let newList = res.data;
                list = isReset ? newList : list.concat(newList);
                this.setData({
                    "_recruitment.list": list,
                    "_recruitment.total": res.count
                });
            } catch (e) {
                this.setData({
                    "_recruitment.error": true
                })
            } finally {
                this.setData({
                    "_recruitment.reset": false,
                    "_recruitment.loading": false
                })
            }
        },
        //上拉加载更多
        onScrollToLower: function () {
            this.setData({"_recruitment.loading": true})
        },

        /*
        * 监听menu切换
        * */
        onMenuChange: function (e) {
            let name = e.currentTarget.dataset.name;
            let index = e.detail;
            let current = this.data[name].current;
            if (index === current) return false;
            let search = this.data._search || {};
            let list = this.data[name].list;
            let value = list[index].search;
            switch (name) {
                case "_position":
                    value ? search.position = value : delete search.position;
                    break
                case "_publisher":
                    value ? search.createUser = value : delete search.createUser;
                    break
                default:
                    break
            }
            this.setData({
                [name + ".current"]: index,
                "_recruitment.reset": true
            })
        },

        /*
        * 跳转查看详情
        * */
        bindNavigatorToDetail: function (e) {
            const that = this;
            let id = e.currentTarget.dataset.id;
            Notification.remove({
                name: Config.notification.deleteRecruitment,
                observer: that
            })
            Notification.addOnce({
                name: Config.notification.deleteRecruitment,
                callback: function () {
                    that.setData({"_recruitment.reset": true})
                },
                observer: that
            })
            wx.navigateTo({url: "/pages/common/recruitment-intro/index?id=" + id})
        },

        /*
        * 跳转 发布职位
        * */
        bindToRelease: function () {
            const that = this;
            wx.navigateTo({
                url: "/pages/common/recruitment-release/index",
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    acceptDataFromOpenedPage: function(data) {
                        data.isChange ? that.setData({
                            "_recruitment.reset": true
                        }) : null
                    }
                },
            })
        },

    }
})
